package com.atguigu.controller;

import com.atguigu.pojo.Schedule;
import com.atguigu.service.ScheduleService;
import com.atguigu.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize, @PathVariable int currentPage) {
        R r=scheduleService.page(pageSize,currentPage);
        log.info("result:{}",r);
        return r;
    }


    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        R r=scheduleService.delete(id);
        return r;
    }


    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){
        if(result.hasErrors()){
            return R.fail("null exist in params");
        }
        R r=scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R change(@Validated @RequestBody Schedule schedule, BindingResult result){
        if(result.hasErrors()){
            return R.fail("null exist in params");
        }
        R r=scheduleService.change(schedule);
        return r;
    }

}
